import setting from "../../setting.js";
// const inquirer = require("inquirer");
import inquirer from "inquirer";
// const fs = require("fs-extra");
import fs from "fs-extra";
// const { resolve } = require("path");
import { resolve } from "path";

import path from "path";

import { fileURLToPath } from "url";
const __filenameNew = fileURLToPath(import.meta.url);
const __dirnameNew = path.dirname(__filenameNew);

const listFilePath = "../../packages/list.json";

const RegxMap = {
  IS_COMP_NAME: /^[A-Z]/,
  IS_COMP_ZH_NAME:
    /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/,
};

//如果遇到驼峰式命名，就会转为短横线式
const kebabCase = (string) =>
  string
    .replace(/([a-z])([A-Z])/g, "$1-$2")
    .replace(/[\s_]+/g, "-")
    .toLowerCase();

//字符串首字母大写
const firstToUpper = (string) => {
  return string.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
};

const func = async () => {
  /**
   * 通过 node 运行该文件时，会在终端内依次提出三个组件信息相关的问题，
   * 并把答案 compName（组件英文名），compZhName （组件中文名）和 compDesc（组件描述）保存在 meta 对象中并导出。
   */
  const meta = await inquirer.prompt([
    {
      type: "input",
      message: "请输入你要新建的组件名（纯英文，大写开头，使用大驼峰式）：",
      name: "compName",
      validate(answer) {
        // @ts-ignore
        const done = this.async();
        const validateRes = RegxMap.IS_COMP_NAME.test(answer);
        if (!validateRes) {
          done("请按要求输入正确的组件名！");
          return;
        }
        const listData = fs.readJSONSync(resolve(__dirnameNew, listFilePath));
        if (listData.find((item) => item.compName === answer)) {
          done("已存在同名组件，请确认后更换名字再重试。");
          return;
        }
        done(null, true);
      },
    },
    {
      type: "input",
      message: "请输入你要新建的组件名（中文）：",
      name: "compZhName",
      validate(answer) {
        // @ts-ignore
        const done = this.async();
        const validateRes = RegxMap.IS_COMP_ZH_NAME.test(answer);
        if (!validateRes) {
          done("请按要求输入正确的组件名！");
          return;
        }
        done(null, true);
      },
    },
    {
      type: "input",
      message: "请输入组件的功能描述：",
      name: "compDesc",
      default: "默认：这是一个新组件",
    },
  ]);
  const { compName } = meta;
  meta.compClassName = kebabCase(compName);

  meta.uiName = setting.name;
  meta.uiNameUpper = firstToUpper(setting.name);
  return meta;
};

export default func;
